﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace WinUI {
    public class PostgreSqlField {
        public PostgreSqlField(XmlNode node) {
            _name = node.Attributes["c1"].InnerText;            

            _notNull = node.Attributes["c3"].InnerText.ToLower() == "yes";
            _primaryKey = node.Attributes["c4"].InnerText.ToLower() == "yes";
            _default = node.Attributes["c5"].InnerText;
            _comment = node.Attributes["c6"].InnerText;

            string dbType = node.Attributes["c2"].InnerText;
            if (dbType == "serial") {
                _dataType = PostgreSqlDataType.Int;
                _javaType = "int";
                _javaDefaultValue = "Integer.MIN_VALUE";
            } else if (dbType == "integer") {
                if (_default.Contains("nextval(")) {
                    _dataType = PostgreSqlDataType.Serial;
                } else {
                    _dataType = PostgreSqlDataType.Int;
                }
                _javaType = "int";
                _javaDefaultValue = "Integer.MIN_VALUE";
            } else if (dbType == "boolean") {
                _dataType = PostgreSqlDataType.Bool;
                _javaType = "boolean";
                _javaDefaultValue = "false";
            } else if (dbType.IndexOf("character varying") == 0) {
                _dataType = PostgreSqlDataType.Character;
                _javaType = "String";
                _javaDefaultValue = "null";

                if (dbType.Length > "character varying".Length) {
                    _maxLength = int.Parse(dbType.Substring("character varying".Length + 1,
                        dbType.Length - "character varying".Length - 2));
                }
            } else if (dbType.Contains("time")) {
                _dataType = PostgreSqlDataType.TimeStamp;
                _javaType = "Date";
                _javaDefaultValue = "null";
            } else if (dbType == "numeric") {
                _dataType = PostgreSqlDataType.Numeric;
                _javaType = "double";
                _javaDefaultValue = "Double.MAX_VALUE";
            }
        }

        private string _name;

        public string Name {
            get { return _name; }
            set { _name = value; }
        }

        private PostgreSqlDataType _dataType;

        public PostgreSqlDataType DataType {
            get { return _dataType; }
            set { _dataType = value; }
        }
        private bool _notNull;

        public bool NotNull {
            get { return _notNull; }
            set { _notNull = value; }
        }
        private bool _primaryKey;

        public bool PrimaryKey {
            get { return _primaryKey; }
            set { _primaryKey = value; }
        }

        private string _default;

        public string Default {
            get { return _default; }
            set { _default = value; }
        }
        private string _comment;

        public string Comment {
            get { return _comment; }
            set { _comment = value; }
        }

        private string _javaType;

        public string JavaType {
            get { return _javaType; }
            //set { _javaType = value; }
        }

        private string _javaDefaultValue;

        public string JavaDefaultValue {
            get { return _javaDefaultValue; }
            set { _javaDefaultValue = value; }
        }

        public string DisplayName {
            get {
                return CodeGenerationUtil.GenDisplayName(_name);
            }
        }

        private int _maxLength = 0;
        public int MaxLength {
            get { return _maxLength; }
            set { _maxLength = value; }
        }

        public string JavaName {
            get {
                return CodeGenerationUtil.GenJavaName(_name);
            }
        }

    }
}
